// pages/nurse/patient_treat_info/patient_treat_info.js
const util = require('../../../utils/util.js');
const api = require('../../../config/api.js');
const app = getApp();

var patientId = -1;

Page({
  /**
   * 页面的初始数据
   */

  data: {
    disabled: false,
    info_temp: {},
    arm:'',
    place:'',
    vein: '',
    orien: '',
    phase: '',
    terminal: '',
    armArray: ['左臂','右臂'],
    armIndex:0,
    placeArray: ['肘上','肘下'],
    placeIndex:0,

    veinArray: ['贵要','肘正中','头部'],
    veinIndex:0,

    orienArray: ['气管隆突上','气管隆突下'],
    orienIndex:0,

    phaseArray: ['首次化疗','二次化疗','三次化疗以上'],
    phaseIndex:0,

    terminalArray: ['脱管','破损','局部感染','CRBSI','堵塞'],
    terminalIndex:0,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('patient treat info onLoad ', options.patientId)

    if(options.patientId){
      patientId = options.patientId;
    }

    this.getTreatPatientInfofromServer();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  submitTreatInfo: function(wxCode) {
        let that = this;
        that.setData({
          disabled: true
        });
        console.log('submitTreatInfo temp_info before = ' + JSON.stringify(that.data.info_temp));

        that.data.info_temp.arm = that.data.armArray[that.data.armIndex];
        that.data.info_temp.place = that.data.placeArray[that.data.placeIndex];
        that.data.info_temp.vein = that.data.veinArray[that.data.veinIndex];
        that.data.info_temp.phase = that.data.phaseArray[that.data.phaseIndex];
        that.data.info_temp.terminal = that.data.terminalArray[that.data.terminalIndex];
        that.data.info_temp.orien = that.data.orienArray[that.data.orienIndex];

        that.setData({
          info_temp: that.data.info_temp
        })

        console.log('submitTreatInfo temp_info after = ' + JSON.stringify(that.data.info_temp));

        wx.request({
          url: api.PatientInfoSubmit,
          data: that.data.info_temp,
          method: 'POST',
          header: {
            'content-type': 'application/json'
          },
          success: function(res) {
            if (res.data.errno == 0) {
              wx.showModal({
                title: '资料已提交',
                content: res.data.errmsg,
                showCancel: false,
                success (res) {
                  if (res.confirm) {
                    console.log('用户点击确定')
                    wx.navigateBack();
                  } else if (res.cancel) {
                    console.log('用户点击取消')
                    wx.navigateBack();
                  }
                }
              });
            } else {
              console.log('sendPatientinfotoServer res = ' + JSON.stringify(res));
              wx.showModal({
                title: '错误信息',
                content: res.data.errmsg,
                showCancel: false
              });
            }
          }
        });
  },

  getTreatPatientInfofromServer: function(){
    let that = this;
    console.log("getTreatPatientInfofromServer with url: " + api.PatientInfoList + ' app.globalData.id ' + patientId);
    util.request(api.PatientInfoList,{
      id: patientId
    }).then(function(res){
      console.log("getTreatPatientInfofromServer" + JSON.stringify(res));
      that.data.info_temp = res.data;

      that.setData({
        info_temp: that.data.info_temp
      })
      console.log("getTreatPatientInfofromServer temp_info = " + that.data.info_temp);
      const armArray = that.data.armArray;

      let info_temp = that.data.info_temp;

      for (let i = 0, len = armArray.length; i < len; ++i) {
        if(armArray[i] == info_temp.arm) {
          that.setData({
            armIndex: i,
          })
        }
      }

      const placeArray = that.data.placeArray;

      for (let i = 0, len = placeArray.length; i < len; ++i) {
        if(placeArray[i] == info_temp.place) {
          that.setData({
            placeIndex: i
          })
        }
      }

      const veinArray = that.data.veinArray;

      for (let i = 0, len = veinArray.length; i < len; ++i) {
        if(veinArray[i] == info_temp.vein) {
          that.setData({
            veinIndex: i
          })
        }
      }

      const orienArray = that.data.orienArray;

      for (let i = 0, len = orienArray.length; i < len; ++i) {
        if(orienArray[i] == info_temp.orien) {
          that.setData({
            orienIndex: i
          })
        }
      }


      const phaseArray = that.data.phaseArray;

      for (let i = 0, len = phaseArray.length; i < len; ++i) {
        if(phaseArray[i] == info_temp.phase) {
          that.setData({
            phaseIndex: i
          })
        }
      }

      const terminalArray = that.data.terminalArray;

      for (let i = 0, len = terminalArray.length; i < len; ++i) {
        if(terminalArray[i] == info_temp.terminal) {
          that.setData({
            terminalIndex: i
          })
        }
      }

    });
  },
  bindArmChange: function(e) {
    console.log('bindArmChange picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      armIndex: e.detail.value
    })
  },
  bindPlaceChange: function(e) {
    console.log('bindPlaceChange picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      placeIndex: e.detail.value
    })
  },
bindVeinChange: function(e) {
    console.log('bindVeinChange picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      veinIndex: e.detail.value
    })
  },
bindOrienChange: function(e) {
    console.log('bindOrienChange picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      orienIndex: e.detail.value
    })
  },
  bindPhaseChange: function(e) {
    console.log('bindPhaseChange picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      phaseIndex: e.detail.value
    })
  },
  bindTerminalChange: function(e) {
    console.log('bindTerminalChange picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      terminalIndex: e.detail.value
    })
  },
  
})